package cn.hejiang.mamis.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import cn.hejiang.mamis.core.dto.AmisCrudDTO;
import cn.hejiang.mamis.core.dto.OptionDTO;
import cn.hejiang.mamis.core.dto.ResultDTO;
import cn.hejiang.mamis.core.dto.SysMenuDTO;
import cn.hejiang.mamis.core.entity.SessionDetails;
import cn.hejiang.mamis.core.entity.SysMenu;
import cn.hejiang.mamis.core.service.ContextService;
import cn.hejiang.mamis.core.service.SysMenuService;
import cn.hejiang.mamis.core.util.EntityUtil;
import cn.hejiang.mamis.core.util.QueryUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@RequestMapping("/mamis/sys/menu")
@RestController
@Slf4j
public class SysMenuController {
	@Autowired
	ContextService contextService;

	@Autowired
	SysMenuService sysMenuService;

	@GetMapping("/list")
	public ResultDTO<AmisCrudDTO<SysMenuDTO>> getMenuList(final HttpServletRequest request) {
		String curSysId = contextService.getSystem().getId();

		// 查询数据库
		final List<SysMenuDTO> menuList = sysMenuService.getMenuList(curSysId);
		final List<SysMenuDTO> menuTree = EntityUtil.plainToTree(menuList);

		// 构造 amis 要求的数据结构
		final AmisCrudDTO<SysMenuDTO> data = new AmisCrudDTO<>();
		data.setItems(menuTree);
		// data.setTotal(sysMenuMapper.selectCount(wrapper));

		// 返回结果集
		return ResultDTO.success(data);
	}

	@PostMapping()
	public ResultDTO saveMenu(final HttpServletRequest request, @RequestBody final SysMenu obj) {
		final SessionDetails session = contextService.getSession(request);

		final SysMenu entity = new SysMenu();

		// 提取数据信息
		entity.setPid(obj.getPid());
		entity.setName(obj.getName());
		entity.setType(obj.getType());
		entity.setIcon(obj.getIcon());
		entity.setUrl(obj.getUrl());
		entity.setPageId(obj.getPageId());
		entity.setSeq(obj.getSeq());

		// 设置更新人员
		entity.setUpdatedBy(session.getCurUserId());

		boolean ret;
		if (StrUtil.isNotBlank(obj.getId())) {
			// update
			UpdateWrapper<SysMenu> updater = QueryUtil.newUpdateWrapper(obj.getId());
			ret = sysMenuService.update(entity, updater);
		} else {
			// insert

			// 设置创建人员
			entity.setCreatedBy(session.getCurUserId());

			// 设置所属关系
			entity.setOwnDeptId(session.getCurDeptId());
			entity.setOwnSysId(contextService.getSystem().getId());

			ret = sysMenuService.save(entity);
		}

		return ret ? ResultDTO.success() : ResultDTO.error("保存失败");
	}

	@DeleteMapping("/{id}")
	public ResultDTO delMenu(final HttpServletRequest request, @PathVariable("id") String id) {
		UpdateWrapper<SysMenu> wrapper = QueryUtil.newDeleteWrapper(id);
		boolean ret = sysMenuService.update(null, wrapper);

		return ret ? ResultDTO.success() : ResultDTO.error("删除失败");
	}

	@GetMapping("/option")
	public ResultDTO<List<OptionDTO>> getMenuOptionList(final HttpServletRequest request) {
		String curSysId = contextService.getSystem().getId();

		// 查询数据库
		final List<SysMenuDTO> menuList = sysMenuService.getMenuList(curSysId);
		final List<SysMenuDTO> menuTree = EntityUtil.plainToTree(menuList);
		final List<OptionDTO> menuOptionTree = EntityUtil.toOptionTree(menuTree);

		// 返回结果集
		return ResultDTO.success(menuOptionTree);
	}
}
